<hr class="layui-bg-gray">
<hr class="layui-bg-gray">

<div style="width: 100%;">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>【结构】同步操作</legend>
    </fieldset>

    <hr class="layui-bg-gray">
    <hr class="layui-bg-gray">

    <table style="width: 100%">
        <tr>
            <td style="text-align: center" id="tempConfigTreeData">
                <div class="layui-btn-container">
                    <button type="button" id="syncNew" class="layui-btn layui-btn-lg layui-btn-normal layui-btn-radius">新表同步(单表)</button>
                </div>
            </td>
            <td style="text-align: center" id="tempTabTreeData">
                <div class="layui-btn-container">
                    <button type="button" id="syncOld" class="layui-btn layui-btn-lg layui-btn-radius">旧表同步(单表)</button>
                </div>
            </td>
            <td style="text-align: center">
                <div class="layui-btn-container">
                    <button type="button" id="syncSomeNew" class="layui-btn layui-btn-lg layui-btn-normal layui-btn-radius">新表同步(多表)</button>
                </div>
            </td>
            <td style="text-align: center">
                <div class="layui-btn-container">
                    <button type="button" id="syncSomeOld" class="layui-btn layui-btn-lg layui-btn-radius">旧表同步(多表)</button>
                </div>
            </td>
            <td style="text-align: center" id="tempSyncLayId">
                <div class="layui-btn-container">
                    <button type="button" id="syncAll" class="layui-btn layui-btn-lg layui-btn-danger layui-btn-radius">全表同步(所有)</button>
                </div>
            </td>
        </tr>
    </table>

</div>

<hr class="layui-bg-gray">
<hr class="layui-bg-gray">


<script>
    // 加载配置列表
    $(function () {
        $.ajax({
            type:'get',
            url:'tableConfig/queryAllConfigVo?conType=1',
            success:function (res) {
                $('#tempConfigTreeData').data('tempConfigTreeData',res.data);
            }
        })
    });

    // 新表同步(单表)
    $(document.body).on('click', '#syncNew', function () {
        // 获取配置列表
        layui.use('layer', function() {
            var $ = layui.jquery, layer = layui.layer;

            var layId = layer.open({
                title:'新表同步',
                id: 'LAY_syncOperation',//设定一个id，防止重复弹出
                type: 1,
                area: ['400px', '230px'],
                shadeClose: true,
                closeBtn: 0,
                btn: ['执行','取消'],
                content: getHtml('/sys/sync/one.html'),
                yes: function(index){
                    // 执行确定逻辑
                    let configId = $('#eConfigId1').val();
                    let tableName = $('#eTabName1').val();

                    if (configId != null && configId.trim() != '' && tableName != null && tableName.trim() != ''){
                        syncNewTable(configId,tableName);

                        layer.close(index);
                    }else {
                        layer.alert('未获取到配置或表');
                    }
                }
                ,btn2: function(index){
                    layer.close(index);
                }
            });

            $('#tempSyncLayId').attr('layId',layId);
        });
    });

    // 新表同步(多表)
    $(document.body).on('click', '#syncSomeNew', function () {
        // 获取配置列表
        layui.use('layer', function() {
            var $ = layui.jquery, layer = layui.layer;

            var layId = layer.open({
                title:'新表同步(选旧表会失败)',
                id: 'LAY_syncSomeNew',//设定一个id，防止重复弹出
                type: 1,
                area: ['400px', '230px'],
                shadeClose: true,
                closeBtn: 0,
                btn: ['执行','取消'],
                content: getHtml('/sys/sync/some.html'),
                yes: function(index){
                    // 执行确定逻辑
                    let configId = $('#eConfigId1').val();
                    let tableName = $('#eTabName2').val();

                    if (configId != null && configId.trim() != '' && tableName != null && tableName.trim() != ''){
                        syncSomeNewTable(configId,tableName);

                        layer.close(index);
                    }else {
                        layer.alert('未获取到配置或表');
                    }
                }
                ,btn2: function(index){
                    layer.close(index);
                }
            });

            $('#tempSyncLayId').attr('layId',layId);
        });
    });

    // 旧表同步
    $(document.body).on('click', '#syncOld', function () {
        // 获取配置列表
        layui.use('layer', function() {
            var $ = layui.jquery, layer = layui.layer;

            var layId = layer.open({
                title:'旧表同步',
                id: 'LAY_syncOperation',//设定一个id，防止重复弹出
                type: 1,
                area: ['400px', '230px'],
                shadeClose: true,
                closeBtn: 0,
                btn: ['执行','取消'],
                content: getHtml('/sys/sync/one.html'),
                yes: function(index){
                    // 执行确定逻辑
                    let configId = $('#eConfigId1').val();
                    let tableName = $('#eTabName1').val();

                    if (configId != null && configId.trim() != '' && tableName != null && tableName.trim() != ''){
                        syncOldTable(configId,tableName);

                        layer.close(index);
                    }else {
                        layer.alert('未获取到配置或表');
                    }

                }
                ,btn2: function(index){
                    layer.close(index);
                }
            });

            $('#tempSyncLayId').attr('layId',layId);
        });
    });

    // 旧表同步(多表)
    $(document.body).on('click', '#syncSomeOld', function () {
        // 获取配置列表
        layui.use('layer', function() {
            var $ = layui.jquery, layer = layui.layer;

            var layId = layer.open({
                title:'旧表同步(多表)',
                id: 'LAY_syncSomeOld',//设定一个id，防止重复弹出
                type: 1,
                area: ['400px', '230px'],
                shadeClose: true,
                closeBtn: 0,
                btn: ['执行','取消'],
                content: getHtml('/sys/sync/some.html'),
                yes: function(index){
                    // 执行确定逻辑
                    let configId = $('#eConfigId1').val();
                    let tableName = $('#eTabName2').val();

                    if (configId != null && configId.trim() != '' && tableName != null && tableName.trim() != ''){
                        syncSomeOldTable(configId,tableName);

                        layer.close(index);
                    }else {
                        layer.alert('未获取到配置或表');
                    }
                }
                ,btn2: function(index){
                    layer.close(index);
                }
            });

            $('#tempSyncLayId').attr('layId',layId);
        });
    });

    // 全表同步
    $(document.body).on('click', '#syncAll', function () {
        // 获取配置列表
        layui.use('layer', function() {
            var $ = layui.jquery, layer = layui.layer;

            var layId = layer.open({
                title:'全表同步',
                id: 'LAY_syncOperation',//设定一个id，防止重复弹出
                type: 1,
                area: ['400px', '200px'],
                shadeClose: true,
                closeBtn: 0,
                btn: ['执行','取消'],
                content: getHtml('/sys/sync/all.html'),
                yes: function(index){
                    // 执行确定逻辑
                    let configId = $('#eConfigId1').val();
                    if (configId != null && configId.trim() != ''){
                        syncAllTable(configId);

                        layer.close(index);
                    }else {
                        layer.alert('未获取到配置');
                    }

                }
                ,btn2: function(index){
                    layer.close(index);
                }
            });

            $('#tempSyncLayId').attr('layId',layId);
        });
    });


    function syncNewTable(configId,tableName) {
        //console.log('新表同步配置ID:'+configId+'  表名:'+tableName);
        $.ajax({
            type:'post',
            url:'mysql/syncNewTable',
            data:{'configId':configId,'tableName':tableName},
            success:function (res) {
                layer.msg(res.desc);
            }
        })
    }


    function syncSomeNewTable(configId,tableName) {
        //console.log('新表同步配置ID:'+configId+'  表名:'+tableName);
        $.ajax({
            type:'post',
            url:'mysql/syncSomeNewTable',
            data:{'configId':configId,'tableName':tableName},
            success:function (res) {
                layer.msg(res.desc);
            }
        })
    }


    function syncOldTable(configId,tableName) {
        //console.log('旧表同步配置ID:'+configId+'  表名:'+tableName);
        $.ajax({
            type:'post',
            url:'mysql/syncOldTable',
            data:{'configId':configId,'tableName':tableName},
            success:function (res) {
                layer.msg(res.desc);
            }
        })
    }


    function syncSomeOldTable(configId,tableName) {
        //console.log('旧表同步配置ID:'+configId+'  表名:'+tableName);
        $.ajax({
            type:'post',
            url:'mysql/syncSomeOldTable',
            data:{'configId':configId,'tableName':tableName},
            success:function (res) {
                layer.msg(res.desc);
            }
        })
    }


    function syncAllTable(configId) {
        //console.log('全表同步配置ID:'+configId);
        $.ajax({
            type:'post',
            url:'mysql/syncAllTable',
            data:{'configId':configId},
            success:function (res) {
                layer.msg(res.desc);
            }
        })
    }

</script>


